#include "stdafx.h"
#include "CPhoneBookStatu.h"
#include "data.h"
#include "CGraphicMgr.h"
#include "MsgBoxStatu.h"
#include "CComingCall.h"
#include "iolib.h"
#include "CPhoneStatu.h"
CPhoneBookStatu::CPhoneBookStatu(WCHAR * name):IStatu(name)
{
		//m_pPhoneBook =new CPhoneBookData();
		m_index=-1;
}

CPhoneBookStatu::~CPhoneBookStatu(void)
{

	//delete m_pPhoneBook;
}


 bool CPhoneBookStatu::EnterStatu()
 {
	 g_PhoneBook->Load(CGenericFun::GetAbsPath(FILE_PHONEBOOK));

	 m_pCellNum=(CEdit*)GRAPHICMGR.GetElement(L"PhoneBookDetail_num");
	 m_pTelNum=(CEdit*)GRAPHICMGR.GetElement(L"PhoneBookDetail_tel");
	 m_pName=(CEdit*)GRAPHICMGR.GetElement(L"PhoneBookDetail_name");
	 m_pMark=(CEdit*)GRAPHICMGR.GetElement(L"PhoneBookDetail_mark");
	 m_pName->SetFocus();

	 TCHAR szLastState[256]={0};
	 GRAPHICMGR.GetLatestStatu(1,szLastState);
	 wstring strLastState=szLastState;
	 //CGenericFun::IME_Change(EIME_ENGLISH);
//if(strLastState==L"PhoneBooklist")
{
	 if (m_index>=0)
	 {
		 m_pCellNum->SetWindowText(g_PhoneBook->GetMobile(m_index));
		 m_pTelNum->SetWindowText(g_PhoneBook->GetTel(m_index));
		 m_pName->SetWindowText(g_PhoneBook->GetName(m_index));
		 m_pMark->SetWindowText(g_PhoneBook->GetHint(m_index));

	 }

	 else  if(m_index==-1)
	 {
		 m_pCellNum->SetWindowText(L"");
		 m_pTelNum->SetWindowText(L"");
		 m_pName->SetWindowText(L"");
		 m_pMark->SetWindowText(L"");

	 }
}
	 return false;
 }

 bool CPhoneBookStatu::ExitStatu()
 {
	 ShowWindow(m_pCellNum->m_hWndEdit,SW_HIDE);
	 ShowWindow(m_pTelNum->m_hWndEdit,SW_HIDE);
	 ShowWindow(m_pName->m_hWndEdit,SW_HIDE);
	 ShowWindow(m_pMark->m_hWndEdit,SW_HIDE);
	  CGenericFun::IME_Change(EIME_OFF);
	 return false;
 }



 bool CPhoneBookStatu::EvaluateClickEvent(const WCHAR* elementName)
 {
	 if (wcscmp(elementName,L"PBDetail_btn_Keyboard") == 0)
	 {
		 // return  DialOut();
				return  CGenericFun::IME_Change(EIME_ENGLISH);

	 }
	 else  if (wcscmp(elementName,L"PhoneBookDetail_btn_close") == 0)
	 {
		 // return  DialOut();
		 GRAPHICMGR.PostChange(L"PhoneBooklist");
		 return false;

	 }
	 else if(wcscmp(elementName,L"PBDetail_btn_Save") == 0)
	 {  
		 
		wstring  strTemp1, strTemp2;
		 strTemp1=m_pName->GetWindowText();
         strTemp2=m_pCellNum->GetWindowText();
        
		 if (strTemp1==L""&&strTemp2==L"")
        {
			return true;
        }
		 
		 if (CGenericFun::IsPhoneNum(strTemp2.c_str()))
		 {
			 CMsgBoxStatu *p= (CMsgBoxStatu*)GRAPHICMGR.GetStatu(L"sub_info");
			 p->SetMsgMode(MSGTEXT);
			 const WCHAR * lang = GlobalStatic::langGetItem(L"MSGNOTALLOW" ,GlobalStatic::langGetCurrentLang());
			 GlobalStatic::MsgBox(lang);
			 CGenericFun::IME_Change(EIME_OFF);
			 return true;

		 }
		 const WCHAR * lang = GlobalStatic::langGetItem(L"MSGSAVE" ,GlobalStatic::langGetCurrentLang());
		 CMsgBoxStatu *p= (CMsgBoxStatu*)GRAPHICMGR.GetStatu(L"sub_info");
		 p->SetMsgMode(MSGSAVEPHONENOTE);
		 GlobalStatic::MsgBox(lang);
		

		/* g_PhoneBook->Add(m_pCellNum->GetWindowText(),m_pTelNum->GetWindowText(),L" ",m_pName->GetWindowText(),L" ",\
			 m_pMark->GetWindowText());

		 g_PhoneBook->Save(CGenericFun::GetAbsPath(FILE_PHONEBOOK));*/
		 return true;

	 }
	 else if(wcscmp(elementName,L"PBDetail_btn_Dial") == 0)
	 {

	
		 CPhoneStatu *p=(CPhoneStatu *)GRAPHICMGR.GetStatu(L"phonedialout");
		 if(p)
			 return p->DialOut(m_pCellNum->GetWindowText());
		/*  wstring  strTemp1;
		 CStaticText *p= (CStaticText *)GRAPHICMGR.GetElement(L"comephonenum");

		 TCHAR szName[MAX_PATH] = {_T('\0')};
		 GetNameFormCellNum(m_pCellNum->GetWindowText(),szName);

		 if(wcslen(szName)!=0)
		 {
			 wcscat(szName,m_pCellNum->GetWindowText());
			 p->setvalue(szName);
		 }
		 else
		 {
			 p->setvalue(m_pCellNum->GetWindowText());
		 }
         strTemp1=m_pCellNum->GetWindowText();
		 if (CGenericFun::IsPhoneNum(strTemp1.c_str()))
		 {
			 CMsgBoxStatu *p= (CMsgBoxStatu*)GRAPHICMGR.GetStatu(L"sub_info");
			 p->SetMsgMode(MSGTEXT);
			 const WCHAR * lang = GlobalStatic::langGetItem(L"MSGNOTALLOW" ,GlobalStatic::langGetCurrentLang());
			 GlobalStatic::MsgBox(lang);
			 CGenericFun::IME_Change(EIME_OFF);
			 return true;

		 }

		  g_pSeries->SendDialCMD((TCHAR*)strTemp1.c_str());
		 CComingCallStatu *pStatu=(CComingCallStatu *)GRAPHICMGR.GetStatu(L"comeingcall");
		 pStatu->SetStatuType(TALK_CALLOUT);
		 CButton *pBtn=(CButton *)GRAPHICMGR.GetElement(L"phonein_btn_pickup");  
		 pBtn->SetEnble(FALSE);
         g_bForeground=false;
		 GRAPHICMGR.PostChange(L"comeingcall");
		 return true;*/

	 }
	 else if(wcscmp(elementName,L"PBDetail_btn_Outmail") == 0)
	 {
         CEdit*p=(CEdit*)GRAPHICMGR.GetElement(L"phonenum");
		 p->SetWindowText(m_pCellNum->GetWindowText());

		 GRAPHICMGR.PostChange(L"pushsms");
		 return true;

	 }
	

	 else if (wcscmp(elementName,L"Weather_statu") == 0)
	 {


		 CTemperatureInfo *pTempratureInfo=(CTemperatureInfo*)GRAPHICMGR.GetElement(L"TempratureInfo");

		 		pTempratureInfo->setvalue(g_pSeries->GetWeatherReport());
				 pTempratureInfo->setvisible(true);
		 KillTimer(GRAPHICMGR.GetWnd(),WEATHER_TIPS);
		 SetTimer(GRAPHICMGR.GetWnd(),WEATHER_TIPS,5000,OnWeatherTipsCount);
         GRAPHICMGR.SetUpdateElements(L"TempratureInfo");

		 return true;
	 }
	
	 return false;
 }
  void  CPhoneBookStatu::SavePhoneNote()
  {
	  if (m_index>=0)

	  {
	
		  g_PhoneBook->Modify(m_index,m_pCellNum->GetWindowText(),m_pTelNum->GetWindowText(),L" ",m_pName->GetWindowText(),L" ",\
			  m_pMark->GetWindowText());
	  }
	  else
	  g_PhoneBook->Add(m_pCellNum->GetWindowText(),m_pTelNum->GetWindowText(),L" ",m_pName->GetWindowText(),L" ",\
		  m_pMark->GetWindowText());

	  g_PhoneBook->Save(CGenericFun::GetAbsPath(FILE_PHONEBOOK));

  }